﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using HtmlAgilityPack;
using Kaixin.Components.Toolkit;
using Kaixin.Components.InterFace;

namespace Kaixin.Components.Net
{
    public class Robot:IAccessPage
    {
        #region Field
        private WebHelper _wh;
        #endregion

        #region Property
        public string Email { get; private set; }
        public string Password { get; private set; }
        #endregion

        public Robot(string email, string pwd)
        {
            Email = email;
            Password = pwd;
            _wh = new WebHelper(Email);
        }

        #region IAccessPage 成员
        /// <summary>
        /// 模拟访问页面
        /// </summary>
        /// <param name="url"></param>
        /// <returns>html的包装类</returns>
        public HtmlDocument AccessPage(string url)
        {
            HtmlDocument doc = _wh.GetHtmlWeb().Load(url);
            if (Common.IsLogin(doc))
            {
                return doc;
            }
            else
            {
                if (Common.Login(Email, Password))
                {
                    return _wh.GetHtmlWeb().Load(url);
                }
                else
                {
                    throw new Exception("账号密码错误");
                }
            }
        }

        #endregion
    }
}
